package w3_c2_queue;

import java.util.Random;

public class Main {
    private static double testQueue(Queue<Integer> q, int opCount) {
        long start = System.nanoTime();
        Random rnd = new Random();
        for (int i = 0; i < opCount; i++)
            q.enqueue(rnd.nextInt(Integer.MAX_VALUE));
        for (int i = 0; i < opCount; i++)
            q.dequeue();
        long end = System.nanoTime();
        double time = (end - start) / 1e9;
        return time;
    }

    /**
     * 进行100000次入队和出队操作，对比三种不同的队列性能，分别是顺序队列、循环队列、链队列。结果为，后两者耗时相当，很短；
     * 而顺序队列因为需要不断分配额外空间，时间为后两者的1000倍。结果为：
     * ArrayQueue, time: 11.8430145 s
     * loopQueue, time: 0.0098545 s
     * linkedListQueue, time: 0.0068751 s
     *
     * @param args
     */
    public static void main(String[] args) {
        double time;
        int n = 100000;
        ArrayQueue<Integer> arrayQueue = new ArrayQueue<>();
        time = testQueue(arrayQueue, n);
        System.out.println("ArrayQueue, time: " + time + " s");

        LoopQueue<Integer> loopQueue = new LoopQueue<>();
        time = testQueue(loopQueue, n);
        System.out.println("loopQueue, time: " + time + " s");

        LinkedListQueue<Integer> linkedListQueue = new LinkedListQueue<>();
        time = testQueue(linkedListQueue, n);
        System.out.println("linkedListQueue, time: " + time + " s");
    }
}
